package com.ruoyi.system.service.house.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.house.HouseInformation;
import com.ruoyi.system.domain.house.HouseInformationDetail;
import com.ruoyi.system.domain.house.vo.SearchSubmitHouseInformationVO;
import com.ruoyi.system.domain.house.vo.SubmitHouseInformationVO;
import com.ruoyi.system.mapper.HouseInformationMapper;
import com.ruoyi.system.service.house.IHouseInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author caiden
 * @Description
 * @email 772149497@qq.com
 * @create 2021-04-05 11:40
 */
@Service
public class HouseInformationServiceImpl implements IHouseInformationService {
    @Autowired
    private HouseInformationMapper houseInformationMapper;


    @Override
    public HashMap<String, Object> checkHouseInformationCode(String code) {
        int count = houseInformationMapper.checkHouseInformationCode(code);
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",200);
        result.put("data",count);
        return result;
    }

    @Override
    @Transactional
    public HashMap<String, Object> addHouseInformation(SubmitHouseInformationVO submitHouseInformationVO) throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        String userId = String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId());
        HouseInformation houseInformation = submitHouseInformationVO.getHouseInformation();
        houseInformation.setCreateDatetime(LocalDateTime.now());
        houseInformation.setCreateBy(userId);
        houseInformation.setId(IdUtils.simpleUUID());
        int insertRow = houseInformationMapper.addHouseInformation(houseInformation);
        if(insertRow > 0){
            List<HouseInformationDetail> houseInformationDetails = submitHouseInformationVO.getHouseInformationDetails();
            for(HouseInformationDetail houseInformationDetail : houseInformationDetails){
                houseInformationDetail.setId(IdUtils.simpleUUID());
                houseInformationDetail.setHouseInformationId(houseInformation.getId());
                houseInformation.setCreateDatetime(houseInformation.getCreateDatetime());
            }
            if(houseInformationDetails.size() > 0){
                houseInformationMapper.addHouseInformationDetail(submitHouseInformationVO.getHouseInformationDetails());
            }
            result.put("code",200);
            return result;
        }
        throw new Exception("添加失败");
    }

    @Override
    public HashMap<String, Object> queryHouseInformationList(SearchSubmitHouseInformationVO searchSubmitHouseInformationVO) {
        HashMap<String, Object> result = new HashMap<>();
        Integer startPosition = searchSubmitHouseInformationVO.getCurrent() <= 1 ? 0 : ((searchSubmitHouseInformationVO.getCurrent() - 1 ) * searchSubmitHouseInformationVO.getSize());
        List<HouseInformation> houseInformations = houseInformationMapper.searchHouseInformation(searchSubmitHouseInformationVO,startPosition);
        int total = houseInformationMapper.countSearchHouseInformation(searchSubmitHouseInformationVO);
        result.put("code",200);
        result.put("data",houseInformations);
        result.put("total",total);
        return result;
    }

    @Override
    public HashMap<String, Object> viewHouseInformationDetailById(String id,SearchSubmitHouseInformationVO searchSubmitHouseInformationVO) {
        HashMap<String, Object> result = new HashMap<>();
        HouseInformation houseInformation = houseInformationMapper.selectHouseInformationById(id);
        if(houseInformation != null){
            Integer startPosition = searchSubmitHouseInformationVO.getCurrent() <= 1 ? 0 : ((searchSubmitHouseInformationVO.getCurrent() - 1 ) * searchSubmitHouseInformationVO.getSize());
            List<HouseInformationDetail> houseInformationDetails = houseInformationMapper.selectHouseInformationDetailByHouseInformationId(id,null,null);
            int total = houseInformationMapper.countHouseInformationDetailByHouseInformationId(id);
            SubmitHouseInformationVO submitHouseInformationVO = new SubmitHouseInformationVO();
            submitHouseInformationVO.setHouseInformation(houseInformation);
            submitHouseInformationVO.setHouseInformationDetails(houseInformationDetails);
            result.put("code",200);
            result.put("data",submitHouseInformationVO);
            result.put("total",total);
        }else{
            result.put("code",400);
            result.put("msg","查无该记录");
        }
        return result;
    }

    @Override
    public HashMap<String, Object> viewHouseInformationDetail(SearchSubmitHouseInformationVO searchSubmitHouseInformationVO) {
        HashMap<String, Object> result = new HashMap<>();
        List<HouseInformation> houseInformations = houseInformationMapper.searchHouseInformation(searchSubmitHouseInformationVO,null);
        for (HouseInformation houseInformation : houseInformations){
            List<HouseInformationDetail> houseInformationDetails = houseInformationMapper.selectHouseInformationDetailByHouseInformationId(houseInformation.getId(),null,null);
            houseInformation.setHouseInformationDetails(houseInformationDetails);
        }
        result.put("code",200);
        result.put("data",houseInformations);
        return result;
    }

    @Override
    public HashMap<String, Object> editHouseInformationDetail(SubmitHouseInformationVO submitHouseInformationVO) {
        HashMap<String, Object> result = new HashMap<>();
        String userId = String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId());
        HouseInformation houseInformation = submitHouseInformationVO.getHouseInformation();
        houseInformation.setLastUpdateBy(userId);
        houseInformation.setLastUpdateDatetime(LocalDateTime.now());

        List<HouseInformationDetail> adds = new ArrayList<>();
        List<HouseInformationDetail> updates = new ArrayList<>();
        List<HouseInformationDetail> deletes = new ArrayList<>();

        List<HouseInformationDetail> submitHouseInformationDetails = submitHouseInformationVO.getHouseInformationDetails();
        List<HouseInformationDetail> houseInformationDetails = houseInformationMapper.selectHouseInformationDetailByHouseInformationId(houseInformation.getId(),null,null);

        //筛选出需要新增的和需要更新的信息
        for(HouseInformationDetail houseInformationDetail : submitHouseInformationDetails){
            if(null == houseInformationDetail.getId()){
                houseInformationDetail.setId(IdUtils.simpleUUID());
                houseInformationDetail.setHouseInformationId(houseInformation.getId());
                houseInformationDetail.setCreateDatetime(LocalDateTime.now());
                adds.add(houseInformationDetail);
                continue;
            }else{
                for(HouseInformationDetail houseInformationDetail1 : houseInformationDetails){
                    if(houseInformationDetail1.getId().equals(houseInformationDetail.getId())){
                        updates.add(houseInformationDetail);
                        break;
                    }
                }
            }
        }
        for(HouseInformationDetail houseInformationDetail1 : houseInformationDetails){
            boolean deleteFlag = true;
            for(HouseInformationDetail houseInformationDetail : submitHouseInformationDetails) {
                if(houseInformationDetail1.getId().equals(houseInformationDetail.getId())){
                    deleteFlag = false;
                    break;
                }
            }
            if(deleteFlag){
                deletes.add(houseInformationDetail1);
            }
        }


        //更新住房信息抬头
        houseInformationMapper.editorHouseInformation(houseInformation);
        //删除
        if(deletes.size() > 0){
            houseInformationMapper.deleteHouseInformationDetail(deletes);
        }
        //更新
        if(updates.size() > 0){
            houseInformationMapper.editorHouseInformationDetail(updates);
        }
        //添加
        if(adds.size() > 0){
            houseInformationMapper.addHouseInformationDetail(adds);
        }
        result.put("code",200);
        return result;
    }

    @Override
    public HashMap<String, Object> deleteHouseInformationDetail(String id) {
        HashMap<String, Object> result = new HashMap<>();
        HouseInformation houseInformation = houseInformationMapper.selectHouseInformationById(id);
        if(houseInformation != null ){
            List<HouseInformationDetail> houseInformationDetails = houseInformationMapper.selectHouseInformationDetailByHouseInformationId(id,null,null);
            houseInformationMapper.deleteHouseInformationById(houseInformation.getId());
            houseInformationMapper.deleteHouseInformationDetail(houseInformationDetails);
            result.put("code",200);
            result.put("msg","删除成功");
        }else{
            result.put("code",400);
            result.put("msg","记录不存在");
        }
        return result;
    }
}
